//
//  TKMultipleImageBrowserViewController.m
//  TKPhotoSelectDemo
//
//  Created by liupm on 16/4/21.
//  Copyright © 2016年 liupm. All rights reserved.
//

#import "TKIMMulImageBrowserViewController.h"
#import "TKIMMulImageBrowserView.h"
@interface TKIMMulImageBrowserViewController ()<TKIMMulImageBrowserViewDelegate>
@property(nonatomic,strong) TKIMMulImageBrowserView *browserView;
@property(nonatomic,strong) UIButton *checkBox;
@property(nonatomic,strong) TKIMPhotoModel *currentModel;
@property(nonatomic,strong) NSMutableArray *disSelectArray;
@end

@implementation TKIMMulImageBrowserViewController

-(void)loadView
{
    [super loadView];
    _disSelectArray = [NSMutableArray array];
    self.view.backgroundColor =[UIColor blackColor];
    self.navigationController.navigationBar.translucent = YES;
    self.navigationController.navigationBar.alpha = 0.8;
    NSString *title = [NSString stringWithFormat:@"%d/%lu",1,(unsigned long)self.selectedArray.count];
    self.navigationItem.title = title;
    if ([TKIMImagePickerSet shareInstance].allowsMultipleSelection)
    {
        UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
        [space setWidth:-10];
        UIBarButtonItem *button =  [[UIBarButtonItem alloc] initWithCustomView:self.checkBox];
        self.navigationItem.rightBarButtonItems = @[space,button];
    }
    else
    {
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(close)];
    }
    
    self.currentModel = [self.selectedArray firstObject];
    self.checkBox.selected = self.currentModel.isSelected;
    [self.view addSubview:self.browserView];
    [self.browserView reloadData:self.selectedArray];
    //禁止自动适应scrollview的内间距
    self.automaticallyAdjustsScrollViewInsets = NO;
}


-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBar.alpha = 1;
    [self.selectedArray removeObjectsInArray:self.disSelectArray];
}


/**
 *  @Author lpm, 16-04-21 16:04:33
 *
 *  @brief  关闭当前控制器
 */
-(void)close
{
    [self dismissViewControllerAnimated:YES completion:nil];
}


/**
 *  @Author lpm, 16-04-21 13:04:24
 *
 *  @brief  事件路由
 *
 *  @param eventName
 *  @param userInfo
 */
-(void)routerEventWithName:(NSString *)eventName userInfo:(NSObject *)userInfo
{
    if([eventName isEqualToString:TKIMImageBrowersHideNavBarEvent])
    {
        [self.navigationController setNavigationBarHidden:[(NSNumber *)userInfo boolValue] animated:NO];
        [[UIApplication sharedApplication] setStatusBarHidden:[(NSNumber *)userInfo boolValue] withAnimation:NO];
        self.navigationController.navigationBar.alpha = 0.8;
    }
    else if([eventName isEqualToString:TKIMImageConfirmEvent])
    {
        [self.selectedArray removeObjectsInArray:self.disSelectArray];
        [[NSNotificationCenter defaultCenter] postNotificationName:TKIMImagePickerCloseEvent object:self.selectedArray];
    }
    else
    {
        [super routerEventWithName:eventName userInfo:userInfo];
    }
}

/**
 *  @Author lpm, 16-04-21 13:04:34
 *
 *  @brief  滑动到某页
 *
 *  @param page
 */
-(void)didScrollToPage:(NSInteger)page
{
    NSString *title = [NSString stringWithFormat:@"%ld/%d",(long)page,self.selectedArray.count];
    self.navigationItem.title = title;
    self.currentModel = self.selectedArray[page-1];
    self.checkBox.selected = self.currentModel.isSelected;
}


#pragma mark 属性

-(TKIMMulImageBrowserView *)browserView
{
    if (!_browserView)
    {
        _browserView = [[TKIMMulImageBrowserView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-HOME_INDICATOR_HEIGHT)];
        _browserView.delegate =self;
    }
    return _browserView;
}


-(UIButton *)checkBox
{
    if (!_checkBox)
    {
        _checkBox =  [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 35, 35)];
        _checkBox.imageEdgeInsets = UIEdgeInsetsMake(5, 5, 5, 5);
        [_checkBox addTarget:self action:@selector(doCheck) forControlEvents:UIControlEventTouchUpInside];
        [_checkBox setImage:[UIImage imageNamed:TKIMPhotoName(@"photo_unchecked")] forState:UIControlStateNormal];
        [_checkBox setImage:[UIImage imageNamed:TKIMPhotoName(@"photo_unchecked")] forState: UIControlStateHighlighted];
        [_checkBox setImage:[UIImage imageNamed:TKIMPhotoName(@"photo_checked")] forState:UIControlStateSelected];
        [_checkBox setImage:[UIImage imageNamed:TKIMPhotoName(@"photo_checked")] forState:UIControlStateSelected | UIControlStateHighlighted];
    }
    return _checkBox;
}


/**
 *  @Author lpm, 16-04-20 22:04:24
 *
 *  @brief  选中图片
 */
-(void)doCheck
{
    if (self.currentModel.isSelected)
    {
        self.currentModel.isSelected = NO;
        self.checkBox.selected = NO;
    }
    else
    {
        self.currentModel.isSelected = YES;
        self.checkBox.selected = YES;
    }
    
    if (self.currentModel.isSelected)
    {
        if ([self.disSelectArray containsObject:self.currentModel])
        {
            [self.disSelectArray removeObject:self.currentModel];
        }
    }
    else
    {
        [self.disSelectArray addObject:self.currentModel];
    }
}

@end
